﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Machine.Entity;
using Machine.Bussiness;
using System.Linq.Expressions;
using Microsoft.AspNetCore.Authorization;
using Machine.Model.ConfigModel;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Machine.Controllers
{
    /// <summary>
    /// 维修记录
    /// </summary>
    [Route("api/[controller]")]
    [Authorize(Roles = "Admin")]
    public class MachineWXDetailController : Controller
    {
        /// <summary>
        /// 保存图片委托
        /// </summary>
        /// <param name="guid">guid</param>
        /// <param name="filePath">路径</param>
        /// <param name="fileName">文件名</param>
        /// <param name="uid">用户ID</param>
        /// <returns></returns>
        private delegate void SaveFileEventHandler(string guid, string filePath, string fileName, string uid);
        private MachineWXDetailBLL BLL = new MachineWXDetailBLL();
        private AttachmentListBLL SaveFileBLL = new AttachmentListBLL();
        /// <summary>
        /// 添加维修记录
        /// </summary>
        /// <param name="entity">维修记录</param>
        /// <returns></returns>
        [HttpPost]
        [Route("Add")]
        public JsonResult Add([FromBody]MachineBXDetail entity = null)
        {
            if (entity == null)
                return Json("参数为空");
            string uid = Request.Headers["Uid"].ToString();
            string role = Request.Headers["Dept"].ToString();
            string guid = Guid.NewGuid().ToString();
            //绑定委托
            SaveFileEventHandler mySaveFile = new SaveFileEventHandler(SaveFileBLL.SaveFile);

            //处理图片
            string webRootPath = BaseConfigModel.WebRootPath;
            string imgs = entity.Img;
            if (imgs != null && imgs != "")
            {
                string[] imgArray = imgs.Split('^');
                int i = 0;
                foreach (string img in imgArray)
                {
                    string filePath = webRootPath + "/" + img;
                    mySaveFile.Invoke(guid, filePath, guid + "-" + i.ToString() + "jpg", uid);
                    i++;
                }
            }
            string finishKind = BaseConfigModel.Configuration["FinishDetailKind"];
            if (finishKind == entity.Kind)
            {
                //结案
                MachineWXBLL WxBLL = new MachineWXBLL();
                Expression<Func<MachineBX, bool>> where = (p => p.WXID == entity.WXID);
                MachineBX wxEntity = WxBLL.GetByWhere(where);
                if (wxEntity.Status != "20")
                {
                    return Json(new { recode = 0, msg = "维修单不是维修状态,不能结案" });
                }
                wxEntity.Status = "30";
                wxEntity.Updater = uid;
                wxEntity.Updatetime = DateTime.Now;
                if (WxBLL.updateEntity(wxEntity) == 0)
                {
                    return Json(new { recode = 0, msg = "状态变更失败" });
                }
            }
            //新增资料
            // entity.Kind = role == "1" ? "10" : "20";
            entity.Appliant = uid;
            entity.AppTime = DateTime.Now;

            entity.Creater = uid;
            entity.CreateTime = DateTime.Now;
            entity.Updater = uid;
            entity.Updatetime = DateTime.Now;
            entity.GUID = guid;
            if (!BLL.Add(entity))
            {
                return Json(new { recode = 0, msg = "新增失败" });
            }
            return Json(new { recode = 1, msg = "新增成功" });
        }

        /// <summary>
        /// 查询维修记录
        /// </summary>
        /// <param name="para">维修编号</param>
        /// <returns></returns>
        [HttpPost]
        [Route("getList")]
        public JsonResult getList([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            string wxid = para["wxid"].ToString();
            string allowKind = BaseConfigModel.Configuration["AllowDetailKind"];
            string finishKind = BaseConfigModel.Configuration["FinishDetailKind"];
            string[] arrayAllowKind = allowKind.Split('^');

            //查询结案资料
            var finishEntity = BLL.GetEntityByKind(wxid,finishKind);

            var List = BLL.GetPageList(wxid, arrayAllowKind);

            return Json(new
            {
                recode = 1
                ,
                finish = finishEntity
                ,
                List = List
            });
        }
    }
}
